package com.gougou.manager.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gougou.common.entity.EasyUIResult;
import com.gougou.common.util.GetData;
import com.gougou.manager.pojo.ItemParamInfo;
import com.gougou.manager.pojo.TbItemParam;
import com.gougou.manager.service.MPTbItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品规则参数 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2018-09-07
 */
@RestController
@RequestMapping("/tbItemParam")
@Transactional
public class TbItemParamController {
    @Autowired
    private MPTbItemParamService itemParamService;


    /****************************
     *
     *分页查询商品模版
     *
     * @param page
     * @param rows
     * @return
     * ***************************
     */
    @Autowired
    private TbItemCatController tbItemCatController;
    @RequestMapping("/getPage")
    public EasyUIResult getPageItemParem(int page,int rows){
        try {
            EasyUIResult easyUIResult = new EasyUIResult();

            EntityWrapper<TbItemParam> itemParamWrapper = new EntityWrapper<TbItemParam>();
            itemParamWrapper.orderBy("created desc");
            //创建一个集合
            ArrayList list=new ArrayList();

            //查询数据
            List<TbItemParam> itemParamlist = itemParamService.selectPage(new Page(page,rows),itemParamWrapper).getRecords();
            for(int i=0;i<itemParamlist.size();i++){
                String s =tbItemCatController.QueryTbItemCatGetName(Integer.parseInt(String.valueOf(itemParamlist.get(i).getItemCatId())));
                ItemParamInfo itemParamInfo = new ItemParamInfo();
                itemParamInfo.setItemCatName(s);
                itemParamInfo.setCreated(itemParamlist.get(i).getCreated());
                itemParamInfo.setId(itemParamlist.get(i).getId());
                itemParamInfo.setItemCatId(itemParamlist.get(i).getItemCatId());
                itemParamInfo.setUpdated(itemParamlist.get(i).getUpdated());
                itemParamInfo.setParamData(itemParamlist.get(i).getParamData());
                list.add(itemParamInfo);
            }
            easyUIResult.setRows(list);

            //查询总条数
            easyUIResult.setTotal((long) itemParamService.selectCount(itemParamWrapper));

            return easyUIResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /************************
     *
     * 根据id查商品模版
     *
     * @param id
     * @return
     * *********************
     */
    @RequestMapping("/getItemParemInfo/{id}")
    public HashMap<Object, Object> getPageItemParem(@PathVariable int id){
        HashMap<Object, Object> objectObjectHashMap = new HashMap<Object, Object>();
        try{
            EntityWrapper<TbItemParam> tbItemParamEntityWrapper = new EntityWrapper<TbItemParam>();
            tbItemParamEntityWrapper.where("item_cat_id={0}",id);
            List<TbItemParam> tbItemParams = itemParamService.selectList(tbItemParamEntityWrapper);
            if(tbItemParams==null || tbItemParams.size()==0){
                objectObjectHashMap.put("code","400");
                return objectObjectHashMap;
            }else{
                objectObjectHashMap.put("code",200);
                objectObjectHashMap.put("data",tbItemParams);
                return objectObjectHashMap;
            }
        }catch (Exception e){
            e.printStackTrace();
            objectObjectHashMap.put("code","500");
            return objectObjectHashMap;
        }
    }

    /**********************************
     * 根据商格参数id删除规格参数信息
     * @param ids
     * @return
     * *********************************
     */
    @RequestMapping("/delete")
    public HashMap<Object, Object> DeleteItemParam(String ids){
        HashMap<Object, Object> objectObjectHashMap = new HashMap<Object, Object>();
        if(!DeleteItemParams(ids)){
            objectObjectHashMap.put("status","500");
            return objectObjectHashMap;
        };
        objectObjectHashMap.put("status","200");
        return objectObjectHashMap;
    }

    @RequestMapping("/add/{id}")
    public void addItemParams(@PathVariable Integer id,String paramData){
        TbItemParam tbItemParam = new TbItemParam();
        tbItemParam.setItemCatId((long)id);
        tbItemParam.setParamData(paramData);

        tbItemParam.setCreated(GetData.getDate());
        tbItemParam.setCreated(GetData.getDate());
        itemParamService.insert(tbItemParam);
    }


    /***********************************
     * 批量删除方法
     * @param ids
     * @return
     * *********************************
     */
    public boolean DeleteItemParams(String ids){
        /*---------------重组ids字符串----------------------*/
        ids=ids.replace(","," or id=");
        EntityWrapper<TbItemParam> objectEntityWrapper = new EntityWrapper<TbItemParam>();
        objectEntityWrapper.where("id="+ids);
        return itemParamService.delete(objectEntityWrapper);
    }











}

